package com.kaifamiao.unifyresulth;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.websocket.server.PathParam;

@Slf4j
@RestController
public class RequestDemo {
    @GetMapping("test")
    public User test() {
        User user = new User();
        user.setId(1L);
        user.setName("kaifamiao");
        user.setAge(18);
        user.setEmail("kaifamiao@gmail.com");
        return user;
    }

    @GetMapping("test2")
    public ResultData<User> test2() {
        User user = new User();
        user.setId(1L);
        user.setName("kaifamiao");
        user.setAge(18);
        user.setEmail("kaifamiao@gmail.com");
        return ResultData.success(user);
    }

    @GetMapping("/hello")
    public String getStr(){
        return "hello,开发喵";
    }

    @GetMapping("error1")
    public User emptyObject(){
        return null;
    }

    @GetMapping("error2")
    public void empty(){
        throw  new RuntimeException("自定义异常");
    }
    @GetMapping("/wrong")
    public int wrong(){
        int i;
        try{
            i = 9/0;
        }catch (Exception e){
            log.error("error: {}"+e.getMessage());

            i = 0;
            throw  new RuntimeException(e.getMessage());
        }
        return i;
    }

    @GetMapping("/error")
    public int error(){
        int i = 9/0;
        return i;
    }
    @GetMapping("/get")
    public String error(String username){
        if(username==null){
            throw new RuntimeException("用户名不能为空");
        }
        return "Hello,"+username;
    }
}
